//
//  FeastDetailViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/11.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit
import Kingfisher

class FeastDetailViewController: UIViewController {
    
    var feast: FeastDetail?
    
    @IBOutlet weak var button: UIButton!
    
    var scrollView: UIScrollView?
    
    var imageScrollView: UIScrollView?
    
    let price: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = AppFont(size: 17)
        return label
    }()
    
    let amount: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = AppFont(size: 13)
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = feast?.name
        
        button.layer.cornerRadius = 3
        button.layer.masksToBounds = true
        
        setBackBarItem()
        
        scrollView = UIScrollView()
        scrollView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 50)
        scrollView?.showsVerticalScrollIndicator = false
        scrollView?.showsHorizontalScrollIndicator = false
        scrollView?.isScrollEnabled = true
        scrollView?.alwaysBounceVertical = true
        scrollView?.autoresizingMask = UIViewAutoresizing.flexibleHeight
        scrollView?.backgroundColor = UIColor.white
//        scrollView?.delegate = self
        
        imageScrollView = UIScrollView()
        let width = self.view.frame.width
        imageScrollView?.frame = CGRect(x: 0, y: 0, width: width, height: width * 2 / 3)
        imageScrollView?.isPagingEnabled = true
        imageScrollView?.isScrollEnabled = true
        imageScrollView?.showsVerticalScrollIndicator = false
        imageScrollView?.showsHorizontalScrollIndicator = false
        imageScrollView?.scrollsToTop = false
        var contentCount = 1
        if let urls = feast?.pictureUrls {
            if urls.count > 0 {
                contentCount = urls.count
                for i in 0...contentCount - 1 {
                    let imageView = UIImageView()
                    imageView.contentMode = .scaleAspectFill
                    imageView.layer.masksToBounds = true
                    imageView.frame = CGRect(x: CGFloat(i) * (imageScrollView?.frame.size.width)!, y: 0, width: (imageScrollView?.frame.size.width)!, height: (imageScrollView?.frame.size.height)!)
                    let url = URL(string: urls[i])
                    imageView.kf.setImage(with: url, placeholder: UIImage(named: "loading"))
                    imageScrollView?.addSubview(imageView)
                }
            } else {
                setDefaultImage()
            }
        } else {
            setDefaultImage()
        }
        imageScrollView?.contentSize = CGSize(width: (imageScrollView?.frame.width)! * CGFloat(contentCount), height: (imageScrollView?.frame.height)!)
        
//        pageControl.numberOfPages = contentCount
//        pageControl.alpha = 0
//        pageControl.currentPage = 0
        scrollView?.addSubview(imageScrollView!)
        
        price.text = "$\(feast?.price ?? 0)/\(LanguageHelper.getString(key: "TABLE"))"
        amount.text = "\(feast?.courses?.count ?? 0) \(LanguageHelper.getString(key: "DISHES"))"

        let textMaxSize = CGSize(width: self.view.bounds.width, height: 20)
        let priceSize = TextSize.textSize(text: price.text!, font: price.font, maxSize: textMaxSize)
        let amountSize = TextSize.textSize(text: amount.text!, font: amount.font, maxSize: textMaxSize)
        
        price.frame = UIEdgeInsetsInsetRect((imageScrollView?.bounds)!, UIEdgeInsetsMake((imageScrollView?.bounds.height)! - 15 - priceSize.height, 15, 15, (imageScrollView?.bounds.width)! - 15 - priceSize.width))
        amount.frame = UIEdgeInsetsInsetRect((imageScrollView?.bounds)!, UIEdgeInsetsMake((imageScrollView?.bounds.height)! - 15 - amountSize.height, (imageScrollView?.bounds.width)! - 15 - amountSize.width, 15, 15))
        scrollView?.insertSubview(price, aboveSubview: imageScrollView!)
        scrollView?.insertSubview(amount, aboveSubview: imageScrollView!)
        
        var mealNameLabels: [UILabel] = []
        
        if let meals = feast?.courses {
            for i in 0...meals.count - 1 {
                let offset = i % 2
                let line = i / 2
                let label = UILabel()
                label.textColor = UIColor.gray
                label.font = AppFont(size: 14)
                label.text = meals[i]
                let textMaxSize = CGSize(width: self.view.bounds.width / 2 - 20, height: 20)
                let labelSize = TextSize.textSize(text: label.text!, font: label.font, maxSize: textMaxSize)
                label.frame = CGRect(x: 15 + (self.view.bounds.width / 2) * CGFloat(offset), y: (imageScrollView?.frame.origin.y)! + (imageScrollView?.frame.height)! + 15 + 35 * CGFloat(line), width: labelSize.width, height: labelSize.height)
                scrollView?.addSubview(label)
                mealNameLabels.append(label)
            }
            let height = mealNameLabels[meals.count - 1].frame.origin.y + mealNameLabels[meals.count - 1].frame.height
//            if height > (scrollView?.contentSize.height)! {
                scrollView?.contentSize = CGSize(width: self.view.frame.width, height: height)
//            }
        }
        
        self.view.addSubview(scrollView!)
        
    }
    
    @IBAction func consult(_ sender: UIButton) {
        self.showPromptView(message: LanguageHelper.getString(key: "QUERY_DISCOUNT"), confirmText: LanguageHelper.getString(key: "OK"))
    }
    
    func setDefaultImage() {
        //沒有附带照片
        let imageView = UIImageView(image: UIImage(named: "ph_long"))
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.frame = CGRect(x: 0, y: 0, width: (imageScrollView?.frame.size.width)!, height: (imageScrollView?.frame.size.height)!)
        imageScrollView?.addSubview(imageView)
    }
    
}
